/*
 * @Descripttion: 列配置类，配合表格升级而升级
 * @version: v2.0
 * @Author: 34786
 * @Date: 2020-02-25 14:55:28
 * @LastEditors: 34786
 * @LastEditTime: 2021-05-27 09:13:18
 */
import {
  editTypeTransform,
  childEditTypeTransform
} from "../components/CommonTable/js/optionHandler";
class TableRow {
  constructor() {
    this.row = [];
  }
  push(col) {
    let colOpt = col.getColOptions();
    this.row.push(colOpt);
  }
  _cols() {
    return this.row;
  }
}

class TableColumns {
  constructor() {
    this.columnOptions = {};
  }
  getColOptions() {
    return this.columnOptions;
  }
  // 列的类型
  _type(type) {
    this.columnOptions.type = type;
    if (type == "radio" || type == "checkbox" || type == "selection") {
      this._align("center", { footer: "center" });
    }
    return this;
  }
  // 列字段名
  _data(data) {
    this.columnOptions.field = data;
    return this;
  }
  // 列标题
  _title(title) {
    this.columnOptions.title = title;
    return this;
  }
  // 列宽度
  _width(width) {
    this.columnOptions.width = width;
    return this;
  }
  // 列是否允许拖动列宽调整大小
  _resizable(resizable) {
    this.columnOptions.resizable = resizable;
    return this;
  }
  // 列是否显示
  _visible(visible) {
    this.columnOptions.visible = visible;
    return this;
  }
  // 将列固定在左侧或者右侧
  _fixed(fixed) {
    this.columnOptions.fixed = fixed;
    return this;
  }
  /**
   * @name: _align
   * @description: 列对齐方式，默认继承table居中
   * @param {string} align: "left"|"center"|"right"
   * @param {object} differents?: 若header或footer对齐方式和列内容不一致，可单独配置 {header: "left"|"center"|"right", footer: "left"|"center"|"right"}
   * @return {object} this
   */
  _align(align, differents = {}) {
    let { header, footer } = differents;
    this.columnOptions.align = align;
    if (header) {
      this.columnOptions.headerAlign = header;
    }
    if (footer) {
      this.columnOptions.headerAlign = footer;
    }
    return this;
  }
  /**
   * @name: _formatter
   * @description: 格式化显示内容
   * @param {function} formatterFn: ({cellValue, row, column}) => void
   * @return {object} this
   */
  _formatter(formatterFn) {
    this.columnOptions.formatter = formatterFn;
    return this;
  }
  /**
   * @name: _seqMethod
   * @description: 自定义索引方法, 只对 type = seq 有效
   * @param {function} seqMethod: ({row, rowIndex, column, columnIndex}) => void
   * @return {object} this
   */
  _seqMethod(seqMethod) {
    this.columnOptions.seqMethod = seqMethod;
    return this;
  }
  /**
   * @name: _sortable
   * @description: 是否允许列排序
   * @param {boolean} sortable
   * @return {object} this
   */
  _sortable(sortable) {
    this.columnOptions.sortable = sortable;
    return this;
  }
  /**
   * @name: _remoteSort
   * @description: 当前列是否开启服务端排序，默认服务端
   * @param {boolean} remote: true(服务排序) | false(关闭服务端排序)
   * @return {object} this
   */
  _remoteSort(remote) {
    this.columnOptions.remoteSort = remote;
    return this;
  }
  /**
   * @name: _sortBy
   * @description: 自定义排序的属性
   * @param {string | array} propertys
   * @return {object} this
   */
  _sortBy(propertys) {
    this.columnOptions.sortBy = propertys;
    return this;
  }
  /**
   * @name: _sortMethod
   * @description: 自定义排序方法
   * @param {function} method
   * @return {object} this
   */
  _sortMethod(method) {
    this.columnOptions.sortMethod = method;
    return this;
  }
  /**
   * @name: _filterable
   * @description: 当前列是否开启筛选
   * @param {boolean} filterable: true | false，默认false不开启
   * @return {object} this
   */
  _filterable(filterable) {
    this.columnOptions.filterable = filterable;
    Object.defineProperty(this.columnOptions, "filterable", {
      enumerable: false
    });
    return this;
  }
  /**
   * @name: _filterSource
   * @description: 配置筛选条件（注：筛选只能用于列表，如果是树结构则过滤根节点）
   * @param {array<object>} options: [{label(显示的值), value(实际的值), checked(默认是否选中), resetValue(重置时的默认值), data(自定义渲染的数据值（当使用自定义模板时可能会用到）)}]
   * @return {object} this
   */
  _filterSource(options) {
    // filters不支持动态，动态数据通过setFilter方法更新
    if (this.columnOptions.filterable) {
      this.columnOptions.filters = options;
    } else {
      alert("请先配置 _filterable");
    }
    return this;
  }
  /**
   * @name: _filterMultiple
   * @description: 筛选是否允许多选
   * @param {boolean} multiple: true | false，默认true
   * @return {object} this
   */
  _filterMultiple(multiple) {
    if (this.columnOptions.filterable) {
      this.columnOptions.filterMultiple = multiple;
    } else {
      alert("请先配置 _filterable");
    }
    return this;
  }
  /**
   * @name: _filterMethod
   * @description: 列的筛选方法，该方法 Function({value, row, column}) 返回值用来决定该行是否显示
   * @param {function} method
   * @return {object} this
   */
  _filterMethod(method) {
    if (this.columnOptions.filterable) {
      this.columnOptions.filterMethod = method;
    } else {
      alert("请先配置 _filterable");
    }
    return this;
  }
  _filterType(type) {
    if (this.columnOptions.filterable) {
      let filterOpt = {};
      filterOpt["name"] = type;
      this.columnOptions.filterRender = filterOpt;
    } else {
      alert("请先配置 _filterable");
    }
    return this;
  }
  _filterData(option) {
    try {
      Object.assign(this.columnOptions.filterRender, option);
    } catch (err) {
      alert("请先调用方法 _filterType(<String>) 设置单元格类型!");
    }
    return this;
  }
  // 是否可编辑
  _editable(editable) {
    this.columnOptions.editable = editable;
    Object.defineProperty(this.columnOptions, "editable", {
      enumerable: false
    });
    return this;
  }
  // 编辑类型定义
  _editType(type = "input") {
    if (this.columnOptions.editable) {
      this.columnOptions.editRender = editTypeTransform(type);
    }
    // else {
    //   alert("请先配置 _editable");
    // }
    return this;
  }
  // 编辑项配置
  _dataSource(option) {
    if (this.columnOptions.editable) {
      try {
        Object.assign(this.columnOptions.editRender, option);
      } catch (err) {
        alert("请先调用方法 _editType(<String>) 设置编辑类型!");
      }
    }
    return this;
  }
  // 默认渲染器类型
  _cellType(type) {
    let cellOpt = new Object();
    cellOpt["name"] = type;
    this.columnOptions.cellRender = cellOpt;
    if (type == "CustomDom") {
      this._align("center", { footer: "center" });
    }
    return this;
  }
  // 默认渲染器配置
  _cellData(option) {
    try {
      Object.assign(this.columnOptions.cellRender, option);
    } catch (err) {
      alert("请先调用方法 _cellType(<String>) 设置单元格类型!");
    }
    return this;
  }
  // 表头分组配置
  _children(optArr) {
    this.columnOptions["children"] = childEditTypeTransform(optArr);
    return this;
  }
  // 其他配置项
  _others(optionObj) {
    Object.assign(this.columnOptions, optionObj);
    return this;
  }
}

export { TableRow, TableColumns };
